<?php // printers.php -- uses a set of conditionals to display available/unavailable printers

// Include important constants and set admin_home (we need to set the variable since file() doesn't interpret constants in pathnames)

require_once("init.php");
$admin_home = ADMIN_HOME;

// We need to create an array of printer names so that we can loop through

$printer_names = array(baskerville, cleveland, lib_iif, jrc_email, lib_1st_flr, lib_2nd_flr, norris2, read2, scilab, 
		       scilib, younker2, helpdesk2, duerer, rose, gencomp2, inslab2, ccl3_bw);

// Get printer toner levels and store to variables, trimming and excess whitespace

foreach($printer_names as $printer) {
	${$printer} = file ("$admin_home/printers/toner_level/{$printer}_level");
	${"{$printer}_level"} = trim(${$printer}[0]);
	}

// Now we have to take care of ESCHER, since it's a bit weird...

$escher_black = file ("$admin_home/printers/toner_level/escher_black_level");
$escher_cyan = file ("$admin_home/printers/toner_level/escher_cyan_level");
$escher_magenta = file ("$admin_home/printers/toner_level/escher_magenta_level");
$escher_yellow = file ("$admin_home/printers/toner_level/escher_yellow_level");

$escher_black_level = trim($escher_black[0]);
$escher_cyan_level = trim($escher_cyan[0]);
$escher_magenta_level = trim($escher_magenta[0]);
$escher_yellow_level = trim($escher_yellow[0]);

/* I'd also like to keep the ESCHER color data here, even though I don't display it; all we'd have to do
   is add the names to the $printer_names_display array, then loop through them

'ESCHER (Cyan)' => $escher_cyan_level,
'ESCHER (Magenta)' => $escher_magenta_level,
'ESCHER (Yellow)' => $escher_yellow_level,); */

// Now we should store the printer and toner information in its own array

$printer_toner = array (
'BASKERVILLE (Noyce 2401)' => $baskerville_level,
'CLEVELAND' => $cleveland_level,
'LIB_IIF' => $lib_iif_level,
'JRC_EMAIL' => $jrc_email_level,
'LIB_1ST_FLR' => $lib_1st_flr_level,
'LIB_2ND_FLR' => $lib_2nd_flr_level,
'NORRIS2' => $norris2_level,
'READ2' => $read2_level,
'SCILAB' => $scilab_level,
'SCILIB' => $scilib_level,
'YOUNKER2' => $younker2_level,
'HELPDESK2' => $helpdesk2_level,
'DUERER (MathLAN)' => $duerer_level,
'ROSE' => $rose_level,
'GENCOMP2' => $gencomp2_level,
'INSLAB2'=> $inslab2_level,
'ESCHER (Black)' => $escher_black_level,
'CCL3_BW' => $ccl3_bw_level);

/* Files for printer status messages */

// Open the list of status messages...

$status_message_files = "$admin_home/printers/status_messages/list_of_status_messages";
$fh = fopen($status_message_files, 'r');

/* ... and loop through them, setting the filename to a variable and its the contents to
the contents of that variable; this starts to get confusing once we use variable variables, 
but I just saved a bunch of money on my car insurance by switching to Geico. There's a great
article on variable variables here: http://au.php.net/language.variables.variable */

while ($status_file = stream_get_line($fh, filesize($status_message_files), "\n")) {
	$$status_file = file ("/home/satherdy/printers/status_messages/$status_file");
	$$status_file = ${$status_file}[0];
	}

// We also need to close these files once we're done writing to them!

// Files for oo_email_status (i.e. should oo emails be sent or not?)

// Open the list of oo_email files...

$oo_email_files = "$admin_home/printers/oo_status/oo_emails/oo_email_files";
$fh = fopen($oo_email_files, 'r');

/* ... and loop through them, setting the filename to a variable and its the contents to
the contents of that variable... */

while ($oo_email_file = stream_get_line($fh, 1024, "\n")) {
	$$oo_email_file = file ("$admin_home/printers/oo_status/oo_emails/$oo_email_file");
	$$oo_email_file = ${$oo_email_file}[0];
	}

// Open the list of paper_status files...

$paper_statuses = "$admin_home/printers/paper_status/list_of_paper_statuses";
$fh = fopen($paper_statuses, 'r');

/* ... and loop through them, setting the filename to a variable and its the contents to
the contents of that variable... */

while ($paper_status_file = stream_get_line($fh, 1024, "\n")) {
        $$paper_status_file = file ("$admin_home/printers/paper_status/$paper_status_file");
        $$paper_status_file = ${$paper_status_file}[0];
        }

// All of our Printer objects are stored in 'objects.php'

include("objects.php");

// Creates an array to store the fact that the lab is open

$open_printers = array ();

print "<div class=\"main image\"> <!-- Start MAIN PRINTERS div -->\n";

// For each Printer object, check open status, times, and other stuff to display correct information

foreach($printer_array as $printer) {
	
	$printer_string = $printer->get_string_rep();

	/* For the LAZIER and AV1 printers, we don't want a toner level, but we also don't want it to be NULL.
	   When the value is NULL, the toner could just be out, or unretrievable */
	
	if ($printer_string == 'av' || $printer_string == 'lazier') {
		$toner_level = 'no_toner';
	}

	else {
		$toner_level = $printer->get_toner_level();
	}

// If the toner level variable isn't empty and is a number equal to 0, the printer is out-of-order

if ((!($toner_level == 'no_toner') && (empty($toner_level) || $toner_level == 0)) || ${"{$printer_string}_paper_status"} == 1) {
	print "
	<div class=\"printer_closed ${printer_string}_printer\">";

	if (isset($_COOKIE['login'])) {
		print "
	<div class=\"popup_change_status\">
        <div class=\"inside\">
        <div class=\"text\">
	<p>You can change the printer status below</p>
	<form action=\"handle_status_change.php\" method=\"post\">
	<p><input type=\"text\" name=\"${printer_string}_oo\" value=\"${"{$printer_string}_oo"}\" />
	<p>If this box is checked, you'll receive out-of-order e-mails; otherwise, you won't
	<input type=\"checkbox\" ";
				if (${"{$printer_string}_email"} == 1) {
					print "checked=\"checked\" name=\"${printer_string}_email\" value=\"1\" /></p>";
				}

				else {
					print " name=\"${printer_string}_email\" value=\"1\" /></p>";
				}
		print "
		<input type=\"submit\" name=\"submit\" value=\"Change Status\" /></p></form>";
	}

	else {
		print "
	<div class=\"popup_closed\">
	<div class=\"inside\">
	<div class=\"text\">
	<p>${"{$printer_string}_oo"}</p>";
	}
	
	print "
	</div> <!-- End \"text\" div -->
	</div> <!-- End \"inside\" div -->
	</div> <!-- End \"popup\" div -->
	</div> <!-- End \"out-of-order printer\" div -->\n";
}

else if ($printer->get_open_status())
	{  
	print "	
	<div class=\"printer_open ${printer_string}_printer\">";

	if (isset($_COOKIE['login'])) {
		print "
	<div class=\"popup_change_status\">
	<div class=\"inside\">
	<div class=\"text\">
	<p>You can change the printer status below</p>
	<form action=\"handle_status_change.php\" method=\"post\">
	<p><input type=\"text\" name=\"${printer_string}_available\" value=\"${"{$printer_string}_available"}\" />
	<input type=\"submit\" name=\"submit\" value=\"Change Status\" /></p></form>";
	}

	else {
	
	print "
	<div class=\"popup_open\">
	<div class=\"inside\">
	<div class=\"text\"><h3><a href=\"" . $printer->get_url() . "\">" . $printer->get_name() . "</a> is ";
	
	/* Check to see if a printer is always open */

	$always_open_status = $printer->get_always_open_status();
	if ($always_open_status) {
		print "always ";
	}

	print "available";

	/* Now, check to see if the toner file exists */

	if (!($toner_level == 'no_toner')) {	
		print " and has $toner_level% toner";

	// Also, if the toner level is at 1%, we should provide an additional message to reassure users

		if ($toner_level == 1) {
			print ". The Helpdesk has been notified and should refill the toner soon";
		}
	}

	print "</h3>
	<p>Location: " . $printer->get_location() . "</p>
	<p>${"{$printer_string}_available"}</p>";
		}

	print "
	</div> <!-- End \"text\" div -->
	</div> <!-- End \"inside\" div -->
	</div> <!-- End \"popup\" div -->
	</div> <!-- End \"available printer\" div -->\n";
	
	// Adds data to an existing array
	$open_printers[] = $printer->get_lab();
	}
else
	{
	print "
	<div class=\"printer_closed ${printer_string}_printer\">";

	if (isset($_COOKIE['login'])) {
		print "
	<div class=\"popup_change_status\">
	<div class=\"inside\">
	<div class=\"text\">
       	<p>You can change the printer status below</p>
	<form action=\"handle_status_change.php\" method=\"post\">
	<p><input type=\"text\" name=\"${printer_string}_unavailable\" value=\"${"{$printer_string}_unavailable"}\" />
	<input type=\"submit\" name=\"submit\" value=\"Change Status\" /></p></form>";
		}
	else {
		print "
	<div class=\"popup_closed\">
	<div class=\"inside\">
	<div class=\"text\">
	<p>${"{$printer_string}_unavailable"}</p>";
		}

	print "
	</div> <!-- End \"text\" div -->
	</div> <!-- End \"inside\" div -->
	</div> <!-- End \"popup\" div -->
	</div> <!-- End \"unavailable printer\" div -->\n";
	}
}

print "</div> <!-- End MAIN PRINTERS div -->\n";

// Now, find and print information about the available printers

	// It it's a break, printers are technically unavailable
	if (HOLIDAY) {
		print "<p>Unless otherwise noted, all printers are unavailable for the break</p>\n";
	}

	// Otherwise, show the available printers
	else {
		$length = count($open_printers);
		$small_printers = array_slice($open_printers, 0, ($length - 1));
		$string_printers = implode(', ', $small_printers);

		// Replace every instance of "The" with "the"
		$string_printers = ereg_replace('The', 'the', $string_printers);
     
		$last_int = ($length - 1);
		$printers = "Currently, printers are available in $string_printers and $open_printers[$last_int]";		
		print "<p class=\"obl\">$printers</p>\n";
	}

?>
